package cn.echase.ecare.common.mybatis.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.time.DateUtils;
import org.dromara.common.core.constant.SystemConstants;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * TODO 让文博创建 2.3.0 的echase-common-mybatis -->
 */
@Deprecated
public class BaseService {

    private static final String[] PARSE_PATTERNS = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                try {
                    setValue(DateUtils.parseDate(text,PARSE_PATTERNS));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    protected <T> TableDataInfo<T> getDataTable(int code,IPage<T> page) {
//        return TableDataInfo.build(code,page);

        return null; // TODO 需做兼容
    }

    protected <T> TableDataInfo<T> getDataTable(IPage<T> page,int code) {
//        return TableDataInfo.build(code,page);

        return null; // TODO 需做兼容
    }

    protected <T> TableDataInfo<T> getDataTable(int code, List<T> list) {
//        return TableDataInfo.build(code,list);

        return null; // TODO 需做兼容
    }

    /**
     * 赋值租户信息
     */
    protected <T> QueryWrapper<T> WrapperBuild(String tenantId) {
        QueryWrapper<T> queryWrapper = Wrappers.query();
        queryWrapper.eq("del_flag", SystemConstants.NORMAL).eq(StringUtils.isNotEmpty(tenantId),"tenant_id",tenantId);
        return queryWrapper;
    }
}
